package testadventure;

import edu.clemson.cs.hamptos.adventure.AdventureCommand;
import edu.clemson.cs.hamptos.adventure.AdventureEngine;
import edu.clemson.cs.hamptos.adventure.AdventureTarget;
import edu.clemson.cs.hamptos.adventure.AdventureWindow;
import edu.clemson.cs.hamptos.adventure.DoNotUnderstandException;
import edu.clemson.cs.hamptos.adventure.VerbStrategyAlt;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;


/**
 *<p>Extends the <code>BaseItem</code> class.</p>
 * @author Nicks
 */
public class ItemTool extends BaseItem {
    /**
     * <p>Takes in a text blob from the script file to initialize the class,
     * afterwards will strip all the used fields from the 
     * <code>StringBuilder</code> instance</p>
     * @param inBlob a properly formatted <code>StringBuilder</code> blob 
     * delimited by semicolons,then newlines, and then spaces, it will be 
     * modified by this constructor
     */
    public ItemTool(StringBuilder inBlob)
    {
        super(inBlob);
        
        Scanner blobScan = new Scanner(inBlob.toString());
        blobScan.useDelimiter(";\r\n");
        
        Scanner verbIOScan = new Scanner(blobScan.next());
        verbIOScan.useDelimiter(" ");
        while (verbIOScan.hasNext())
        {
            String text = verbIOScan.next();
            if (text.length() > 0)
            {
                VerbStrategyAlt vsa = TheAdventure.verbMap.get(text);
                if(vsa == null)
                    break;
                Set<String> verbs = vsa.getVerbs();
                VContainer vc = new VContainer(vsa, "y", "n");
                Iterator<String> it = verbs.iterator();
                while(it.hasNext())
                {
                    accIOVerbs.put(it.next(), vc);
                }
            }
        }
        
        //removes read fields from inBlob
        inBlob.setLength(0);
        while (blobScan.hasNext())
        {
            inBlob.append(blobScan.next()).append(";\r\n");
        }
    }
   /**
     * <p>Constructs and new <code>ItemTool</code>.
     */
    protected ItemTool() {
        
    }
    
    @Override
    public void doCommandWith(AdventureCommand c, AdventureEngine e, 
                            AdventureWindow w) 
                            throws DoNotUnderstandException {
        //check to see if the verb is acceptable to the io and do
        BaseItem thedo = (BaseItem)c.getDirectObject();
        
        String verb = c.getVerb();
        
        if (!(checkVerbAsIO(verb)))
        {
            throw new DoNotUnderstandException(c, "You can't " + verb 
                                               + " with " + getShortDescription());
        }
        //check to see if io is acc. to do
        if (!(thedo.checkIO(getID())))
        {
            throw new DoNotUnderstandException(c, "You can't " + verb + " "
                                               + thedo.getShortDescription()
                                               + " with " + getShortDescription());
        }
        
        //act
        accIOVerbs.get(verb).getVerbStrategy().doCommand(c, e, w);
        
        thedo.notifyListeners(e, w);
    }
    /**
     * <p></p>
     * @param cl 
     */
    protected void fillClone(ItemTool cl)
    {
        super.fillClone(cl);
    }
    
    @Override
    public AdventureTarget clone()
    {
        ItemTool cl = new ItemTool();
        fillClone(cl);
        
        return cl;
    }
    
    @Override
    public void listen(AdventureEngine e, AdventureWindow w)
    {
        if (stats.containsKey("charges"))
        {
            if (stats.get("charges") > 0)
            {
                int newval = stats.get("charges") - 1;
                stats.put("charges", newval);
                w.println(deactivatedTxt);
                if(newval == 0)
                {
                    stats.put("enabled", 0);
                    w.println(disabledTxt);
                }
            }
        }
        else
        {
            if (stats.get("enabled") == 0)
            {
                stats.put("enabled", 1);
                w.println(activatedTxt);
            }
            else
            {
                stats.put("enabled", 0);
                w.println(deactivatedTxt);
            }
        }
    }
}
